import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login.vue'
import Home from '../views/home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: Login
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  { // 首页
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [{
      path: '/welcome',
      component: () => import('@/views/welcome.vue')
    },
    {
      path: '/permission',
      component: () => import('@/views/components/power/right.vue')
    },
    {
      path: '/roles',
      component: () => import('@/views/components/power/roles.vue')
    },
    { // 数据统计
      path: '/report',
      component: () => import('@/views/report')
    },
    { // 用户管理
      path: '/user',
      component: () => import('@/views/user')
    }]
  }
]

const router = new VueRouter({
  mode: 'hash',
  routes
})

router.beforeEach((to, from, next) => {
  // to: 将要访问
  // from：从哪个路径来
  // next：放行
  if (to.path === '/login') return next()
  // 获取token
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
